import myType from "./myType";
import myObj from "./myObj";
let $axios = require('axios')
$axios.defaults.baseURL = 'http://localhost:8443/api'
const $mytype=myType.mytype;
const $myobj=myObj.myobj;
const date={
  getDate(num){
    let now= new Date();
    if(num==0)
      return now.getFullYear().toString()+'-'+(now.getMonth()+1).toString()
        +'-'+now.getDate().toString()+' '+now.getHours().toString()+':'+now.getMinutes().toString()
        +':'+now.getSeconds();
    return now.getFullYear().toString()+'-'+(now.getMonth()+1).toString()
      +'-'+now.getDate().toString()
  }
}
const money={
  /**
  *@Author Anqing Liang
  *@Description 根据状态表返回活动类型
  *@Date 18:29 2021/2/21
  *@Param
  *@Return
  **/
  getDiscountType(tagList){
    console.log(JSON.stringify(tagList));
    console.log(JSON.stringify(typeof tagList))
    let resp=$mytype.DEFAULT;
      Array.from(tagList).forEach(item=>{
        console.log(JSON.stringify(item.str===$mytype.produceType.FULLMINUS.name))
        switch (item.str) {
          case $mytype.produceType.FULLMINUS.name:
            console.log("FULLMINUS");
            resp=$mytype.produceType.FULLMINUS;
            break;
          default:break;
        }
      })
    return resp;
  },
  /**
  *@Author Anqing Liang
  *@Description 传入fullMinus{} 与good{} ,更新fullMinus{}并返回
  *@Date 14:49 2021/2/22
  *@Param
  *@Return
  **/
  fullMinus(fullMinus,good){
    if(good==null || fullMinus==null){
      return null;
    }
    let totalPrice=good.num*good.actualPrice;
  },
}
const  func={
  /**
   * 可以进行前缘触发的防抖函数
   * @param fn
   * @param delay
   * @param immediate
   * @returns {function(): void}
   */
  debounce(fn,delay,immediate=false){
    let timer = null; //借助闭包
    let flag = true;
    return function() {
      if(timer){
        clearTimeout(timer)
      }
      let self=this;
      if(immediate){
        if(flag){
          fn.apply(this,arguments);
          flag=false;
        }
        timer = setTimeout(function(){
          flag = true
        },delay)
      }else{
        timer= setTimeout(function(){
          fn.apply(self,arguments)
        },delay)
      }
    }
  },

  /**
   * 根据时间生成毫秒级id
   * @returns {string}
   */
  getId(){
    let now=new Date();
    let id=`${now.getFullYear()}
            ${now.getMonth()+1<10?'0':''}
            ${now.getMonth()+1}
            ${now.getDate()<10?'0':''}
            ${now.getDate()}
            ${now.getHours()<10?'0':''}
            ${now.getHours()}
            ${now.getMinutes()<10?'0':''}
            ${now.getMinutes()}
            ${now.getSeconds()<10?'0':''}
            ${now.getSeconds()}
            ${now.getMilliseconds()<10?'00':''}
            ${now.getMilliseconds()>=10 && now.getMilliseconds()<100?'0':''}
            ${now.getMilliseconds()}`.replace(/[\r\n]/g,'').replace(/\ +/g,'').trim();
    return id;
  },
  /**
   * 返回上一级菜单
   * @param _this
   */
  back(_this){
    _this.$router.back();
  },
  /**
   * 权限判断
   * @param name
   */
  isAllow(Pname,_this){
    let staff=_this.$store.state.staff;
    if(staff.Proot){
      return{
        value: true,
        meaning: '允许',
      }
    }
    if(Pname!='Proot'&&staff.Pimpower){
      return{
        value: true,
        meaning: '允许',
      }
    }
    let resp={
      value:staff[Pname]||false,
      meaning:'允许',
    };
    if(!resp.value){
      resp.meaning=staff[Pname]==null?'请登录工作账号':'您无此权限！';
    }
    console.log(resp)
    return resp;
  },
  /**
   * 判断对象是否为空
   * @param obj
   * @returns {boolean}
   */
  isEmpty(obj){
    if(obj==undefined){
      return true;
    }
      //判断参数是否是字符串，去空，如果长度为0则返回true
      if(typeof obj == "string") {
        if(obj.trim().length == 0) {
          return true;
        } else {
          return false;
        }
      }
      if(typeof obj == 'object') {
        //判断参数是否是数组，数组为空则返回true
        if(obj instanceof Array) {
          if(obj.length == 0) {
            return true;
          } else {
            return false;
          }
        }
        //判断参数是否是对象，判断是否是空对象，是则返回true
        if(obj instanceof Object) {
          //判断对象属性个数
          if(Object.keys(obj).length==0){
            return true;
          }
          return false;
        }
      }
  }
}
const _get={
  //GET请求

  //商品加载
    loadGoods(kind){
      return new Promise((resolve, reject) => {
        $axios.get('/goods/goods/'+kind).then(resp=>{
          if(resp && resp.status == 200){
            let _goodsList = resp.data
            resolve(_goodsList);
          }
        })
      })
    },
  /*
  *@Author Anqing Liang
  *@Description
  *@Date 15:10 2021/2/18
  *@Param query
  *@Return good
  **/
    //selectGoodByQuery(query){
    //  console.log('请求URL=/api/goods/goods/'+query);
    //  return query==''? null : new Promise((resolve, reject) => {
    //    $axios.get('/goods/goods/'+query).then(resp=>{
    //      if(resp && resp.status==200){
    //        let good=resp.data;
    //        console.log(JSON.stringify(good));
    //        resolve(good);
    //    }
    //    })
    //  });
    //},

    findSupplierByName(query){
      console.log(query);
      if(query==''){
        query='noQuery';
      }
      return new Promise((resolve, reject) => {
        $axios.get('/setting/supplier/'+query+'/info').then(resp=>{
          if(resp && resp.status==200){
            let supplier = resp.data;
            console.log(JSON.stringify(supplier));
            resolve(supplier);
          }
        })
      })
    },
    findFullMinusByTimeOrId(query){
      if(query==''){
        query='noQuery';
      }
      console.log('请求url=/api/discount/fullMinus/'+query)
      return new Promise((resolve, reject) => {
        $axios.get('/discount/fullMinus/'+query).then(resp=>{
          if(resp && resp.status==200){
            let fullMinus=resp.data;
            console.log(JSON.stringify(fullMinus));
            resolve(fullMinus);
          }
        })
      })
    },
  findFullMinusByIdAndTotal(id,total){
      return new Promise((resolve, reject) => {
        $axios.get('/discount/fullMinus/'+id+'/'+total).then(resp=>{
          if(resp && resp.status==200){
            let fullMinus=resp.data;
            resolve(fullMinus);
          }
        })
      })
  }
}


const _post={
  //POST

  //达标优惠提交
  submitFullMinus(fullMinus){
    return new Promise((resolve, reject) => {
      $axios.post('/discount/fullMinus/add',fullMinus).then(resp=>{
        if(resp &&resp.status==200){
          resolve(true);
        }
      })
    })
  },
  findInventoryGoodByQuery(info){
    return new Promise((resolve, reject) => {
      $axios.post('/inventory/goods/find?info='+info).then(resp=>{
        if(resp && resp.status==200){
          let good=resp.data[0];
          console.log(JSON.stringify(good))
          resolve(good);
        }
      })
    })
  },
  /*
*@Author Anqing Liang
*@Description
*@Date 14:23 2021/2/24
*@Param
*@Return
**/
  selectGoodByQueryWithState(query,state){
    return query==''? null : new Promise((resolve, reject) => {
      $axios.post('/goods/goods/state',{
        query:query,
        name:state.str,
        key:state.key
      }).then(resp=>{
        if(resp && resp.status==200){
          let goods=resp.data;
          resolve(goods);
        }
      })
    })
  },
  /*
  *@Author Anqing Liang
  *@Description 根据商品条码获取活动信息
  *@Date 18:51 2021/2/21
  *@Param
  *@Return
  **/
  getFullMinusByGoodBarcode(barcode){
    return new Promise((resolve, reject) => {
      $axios.post('')
    })
  },

}
const  install = function (vm,options) {
  vm.prototype._date=date;
  vm.prototype._func=func;
  vm.prototype._get=_get;
  vm.prototype._post=_post;
  vm.prototype._mytype=$mytype;
  vm.prototype._money=money;
  vm.prototype._myobj=$myobj;
}
export default {install}
